//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.多米诺骨牌;

public class demo_1007 {
    public demo_1007() {
    }

    public static void main(String[] args) {
        int[] tops = new int[]{1, 2, 1, 1, 1, 2, 2, 2};
        int[] bottoms = new int[]{2, 1, 2, 2, 2, 2, 2, 2};
        System.out.println(minDominoRotations(tops, bottoms));
    }

    public static int minDominoRotations(int[] tops, int[] bottoms) {
        int top0 = tops[0];
        int bottom0 = bottoms[0];
        boolean topOK = true;
        boolean bottomOK = true;
        int top0s = 1;
        int bottom0s = 1;
        int n = tops.length;

        for(int i = 1; i < n; ++i) {
            int top = tops[i];
            int bottom = bottoms[i];
            if (topOK && top0 != top && top0 != bottom) {
                topOK = false;
            }

            if (bottomOK && bottom0 != top && bottom0 != bottom) {
                bottomOK = false;
            }

            if (!topOK && !bottomOK) {
                return -1;
            }

            if (topOK && top0 == top) {
                ++top0s;
            }

            if (bottomOK && bottom0 == bottom) {
                ++bottom0s;
            }
        }

        System.out.println(top0s);
        System.out.println(bottom0s);
        if (topOK && bottomOK) {
            return Math.min(Math.min(top0s, n - top0s), Math.min(bottom0s, n - bottom0s));
        } else if (topOK) {
            return Math.min(top0s, n - top0s);
        } else {
            return Math.min(bottom0s, n - bottom0s);
        }
    }
}
